package com.kalua.list.dbaccess.product;

import static com.kalua.list.dbaccess.DBUtils.getNumericIdFrom;

import java.util.Set;

import org.springframework.cassandra.core.RowMapper;

import com.datastax.driver.core.Row;
import com.datastax.driver.core.exceptions.DriverException;
import com.kalua.list.domainmodel.NumericId;
import com.kalua.list.domainmodel.TextIdentifier;
import com.kalua.list.domainmodel.product.Product;
import com.kalua.list.domainmodel.product.ProductImpl;

public class ProductRowMapper implements RowMapper<Product> {

	public static final String PRODUCT_COL_FAMILY = "product";
	
	public static final String PRODUCT_ID_COL_NAME = "product_id";
	public static final String RETAILER_ID_COL_NAME = "retailer_id";
	public static final String RETAILER_ISSUED_ID_COL_NAME = "retailer_issued_id";
	public static final String PRODUCT_NAME_COL_NAME = "product_name";
	public static final String PRICE_COL_NAME="price"; 
	public static final String FULL_IMAGE_URL_COL_NAME = "full_image_url";
	public static final String THUMBNAIL_IMAGE_URL_COL_NAME = "thumbnail_image_url";
	public static final String CLASSIFICATIONS_COL_NAME = "classifications";
	public static final String BATCH_CREATED_COL_NAME = "batch_created";
	public static final String BATCH_LAST_UPDATED_COL_NAME = "batch_last_updated";
	
	public static final String[] ALL_PRODUCT_COLUMNS = {
		PRODUCT_ID_COL_NAME, RETAILER_ID_COL_NAME,
		RETAILER_ISSUED_ID_COL_NAME, PRODUCT_NAME_COL_NAME,
		PRICE_COL_NAME, FULL_IMAGE_URL_COL_NAME,
		THUMBNAIL_IMAGE_URL_COL_NAME, CLASSIFICATIONS_COL_NAME, 
		BATCH_CREATED_COL_NAME, BATCH_LAST_UPDATED_COL_NAME };
			
	
	@Override
	public Product mapRow(Row row, int rowNum) throws DriverException {
		ProductImpl result;
		
		NumericId id = new NumericId(row.getLong(PRODUCT_ID_COL_NAME));
		TextIdentifier retailerId=new TextIdentifier(row.getString(RETAILER_ID_COL_NAME));
		TextIdentifier retailerIssuedId = new TextIdentifier(row.getString(RETAILER_ISSUED_ID_COL_NAME));		
		NumericId batchCreatedId = getNumericIdFrom(row,BATCH_CREATED_COL_NAME);
		String name = row.getString(PRODUCT_NAME_COL_NAME);
		double price = row.getDecimal(PRICE_COL_NAME).doubleValue();
		String fullImageURL = row.getString(FULL_IMAGE_URL_COL_NAME);
		String thumbnailImageURL = row.getString(THUMBNAIL_IMAGE_URL_COL_NAME);
		
		Set<String> classifications = row.getSet(CLASSIFICATIONS_COL_NAME, String.class);		
		NumericId batchLastUpdated = getNumericIdFrom(row, BATCH_LAST_UPDATED_COL_NAME);
		
		result = new ProductImpl(id,retailerId, batchCreatedId, retailerIssuedId, 
												name, price, fullImageURL, thumbnailImageURL);
		
		result.addClassications(classifications);
		result.setBatchLastUpdated(batchLastUpdated);
		return result;
	}
}